home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 32 / Mac Magazin and MacEasy Magazine CD - Issue 32.iso / Office / ToDo List ƒ / Example AppleScripts / Internet URL Example / Bookmark Extractor Script < prev    next >
Text File  |  1996-06-06  |  3KB  |  94 lines

  1.  
  2.  
  3. on open fileNameAlias
  4.     -- Read in the contents of the HTML file
  5.     set fileAccesObj to (open for access fileNameAlias)
  6.     set fileContentsText to read fileAccesObj
  7.     close access fileAccesObj
  8.     
  9.     -- open a new ToDo List document to put the anchors in
  10.     tell application "ToDo List"
  11.         -- Create a new document to put the items into
  12.         set docNum to make new document
  13.         
  14.         -- Get the name and use it instead since the number could
  15.         -- change if the user makes or opens another document in ToDo List
  16.         -- while we're running
  17.         --set docName to the name of document docNum
  18.         
  19.         -- Set some of the document settings to values more appropriate
  20.         -- to a list of bookmarks
  21.         set the show calendar of document docNum to false
  22.         set the use dates of document docNum to false
  23.         set the clipboard text only of document docNum to true
  24.     end tell
  25.     
  26.     -- Go through it and look for anchors
  27.     set i to offset of "<A" in fileContentsText
  28.     if i = 0 then set i to the offset of "<a" in fileContentsText
  29.     repeat while i > 0
  30.         -- Find the end of the anchor
  31.         set j to the offset of "</A>" in fileContentsText
  32.         if j = 0 then set j to the offset of "</a>" in fileContentsText
  33.         if j = 0 then exit repeat
  34.         
  35.         -- Remove the anchor from the contents string
  36.         set theAnchor to text from character (i + 3) to character (j - 1) of fileContentsText
  37.         set fileContentsText to text from character (j + 4) to end of fileContentsText
  38.         
  39.         -- Find the URL in the anchor
  40.         set k to the offset of "HREF=" in theAnchor
  41.         set theAnchor to text from character (k + 6) to end of theAnchor
  42.         set m to the offset of "\"" in theAnchor
  43.         set theURL to text from character 1 to character (m - 1) of theAnchor
  44.         
  45.         -- Also find the actual tag, since it's usually more meaningful
  46.         -- We need to go backward from the end of the anchor and find the ">" that
  47.         -- closed the <A
  48.         set x to the count of characters in theAnchor
  49.         repeat while character x of theAnchor is not ">" and x > 0
  50.             set x to x - 1
  51.         end repeat
  52.         if (x > 0) then
  53.             set theURL to StripWhitespace(text from character (x + 1) to end of theAnchor) & " (" & theURL & ")"
  54.         end if
  55.         
  56.         tell application "ToDo List"
  57.             -- Put the item at the end of the list by just picking a very large number
  58.             make new item at 32000 with properties {description:theURL} within document docNum -- docName
  59.         end tell
  60.         
  61.         set i to offset of "<A" in fileContentsText
  62.         if i = 0 then set i to the offset of "<a" in fileContentsText
  63.     end repeat
  64.     
  65. end open
  66.  
  67.  
  68. on StripWhitespace(theStr)
  69.     
  70.     -- First, remove any carriage returns within the string
  71.     repeat with x from 1 to (the count of characters in theStr)
  72.         if character x of theStr is return then
  73.             set theStr to (text from character 1 to character (x - 1) of theStr) & space & (text from character (x + 1) to end of theStr)
  74.         end if
  75.     end repeat
  76.     
  77.     -- Get rid of any leading whitespace
  78.     set x to 1
  79.     repeat while character x of theStr is space or character x of theStr is return or character x of theStr is tab
  80.         set x to x + 1
  81.     end repeat
  82.     if x > 1 then set theStr to text from character x to end of theStr
  83.     
  84.     -- Remove any trailing whitespace
  85.     set x to the count of characters in theStr
  86.     repeat while x > 0 and (character x of theStr is space or character x of theStr is return or character x of theStr is tab)
  87.         set x to x - 1
  88.     end repeat
  89.     if (x < the (count of characters in theStr)) then set theStr to text from character 1 to character x of theStr
  90.     
  91.     -- And finally, return the stripped string
  92.     return theStr
  93.     
  94. end StripWhitespace